package fr.resasante.spring3.interceptor;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.logging.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import fr.resasante.utils.PropertiesUtils;

/**
 * Class used for common displays in spring MVC views
 * 
 * @author GEFFROY-DE-VILLEBLAN
 * 
 */
public class MyInterceptor extends HandlerInterceptorAdapter {
	/**
	 * The logger
	 */
	private static final Logger logger = Logger.getLogger(MyInterceptor.class.getName());

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#
	 * afterCompletion(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse, java.lang.Object,
	 * java.lang.Exception)
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
			Object handler, Exception ex) throws Exception {
		logger.debug("After completion handled.");
	}

	/**
	 * Get the URL of the specific Javascript file for the view if exists
	 * 
	 * @param springViewName
	 *            The view name
	 * @param staticsUrl
	 *            The URL hosting static resources
	 * @return The javascript URL
	 */
	private String getJavascriptUrl(String springViewName, String staticsUrl) {
		final String urlString = staticsUrl + "/js/" + springViewName + ".js";
		try {
			final URL url = new URL(urlString);
			new BufferedReader(new InputStreamReader(url.openStream()));
			return urlString;
		} catch (final Exception exc) {
			// logger.error(CommonUtils.getInstance().getStackTrace(exc));
		}
		return "";
	}

	/**
	 * Get the URL of the specific CSS file for the view if exists
	 * 
	 * @param springViewName
	 *            The view name
	 * @param staticsUrl
	 *            The URL hosting static resources
	 * @return The CSS URL
	 */
	private String getStyleUrl(String springViewName, String staticsUrl) {
		final String urlString = staticsUrl + "/css/" + springViewName + ".css";
		try {
			final URL url = new URL(urlString);
			new BufferedReader(new InputStreamReader(url.openStream()));
			return urlString;
		} catch (final Exception exc) {
			// logger.error(CommonUtils.getInstance().getStackTrace(exc));
		}
		return "";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.handler.HandlerInterceptorAdapter#postHandle
	 * (javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse, java.lang.Object,
	 * org.springframework.web.servlet.ModelAndView)
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler, ModelAndView modelAndView) throws Exception {
		if (modelAndView != null) {
			final String staticsUrl = PropertiesUtils.getInstance().getProperty("statics.url")
					.replaceAll("/$", "");
			final String springViewName = modelAndView.getViewName();
			modelAndView.addObject("staticsUrl", staticsUrl);
			modelAndView.addObject("springViewName", springViewName);
			final String styleUrl = getStyleUrl(springViewName, staticsUrl);
			if (!"".equals(styleUrl)) {
				modelAndView.addObject("styleUrl", styleUrl);
			}
			final String javascriptUrl = getJavascriptUrl(springViewName, staticsUrl);
			if (!"".equals(javascriptUrl)) {
				modelAndView.addObject("javascriptUrl", javascriptUrl);
			}
			final boolean recaptchaEnabled = Boolean.parseBoolean(PropertiesUtils
					.getInstance().getProperty("recaptcha.enabled"));
			modelAndView.addObject("recaptchaEnabled", recaptchaEnabled);
		}
		logger.debug("Post-processing handled.");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.handler.HandlerInterceptorAdapter#preHandle
	 * (javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		logger.debug("Pre-processing handled.");
		return true;
	}
}
